package gis.activities;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.Toast;

import com.xkh.xkh.R;

import java.io.File;
import java.util.ArrayList;

import gis.network.http.UploadPhotoAndPoints;
import gis.utils.Constants;
import gis.utils.UploadPhotoPointsThread;
import gis.utils.UploadUtil;
import gis.utils.MyBitmapStore;
import gis.views.beans.PointXY;
import managers.UIHandler;
import managers.UserInfo;

/**
 * 上传照片
 */
public class UploadPhoto extends UIHandler.BaseActivity {


    public ImageView img_View;
    UploadUtil httpUpload ;
    Button upButton_BMP, abortButton, upButton_JPG;
    //String filePath="";
    public static int flag = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.upload_photo);
        img_View = (ImageView) this.findViewById(R.id.imageView);
        Bitmap bmp = MyBitmapStore.getBitmap();
        img_View.setImageBitmap(bmp);


        abortButton = (Button) findViewById(R.id.btn_abort);
        abortButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        final CheckBox checkBox = (CheckBox) findViewById(R.id.checkbox);
        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (checkBox.isChecked() == true) {
                    flag = 1;
                }
            }
        });
        Button button_upload = (Button) findViewById(R.id.btn_upload);
        button_upload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // TODO
//                if (flag == 1) {
//                    File file = new File(MapShow.filePath);
//                    sendPhotoPoints(file);
//
//                } else {
//                    File file = new File(MapShow.filePathBitmap);
//                    sendPhotoPoints(file);
//                }
            }
        });


    }

    void sendPhotoPoints(File file)
    {
        UploadPhotoAndPoints.Upload upload = new UploadPhotoAndPoints.Upload();
        PointXY pointXY = new PointXY();

        //TODO
//        Point2D point2D = MapShow.getLocation();
//        pointXY.setX(point2D.getX() + Constants.LONGITUDE_OFFSET);
//        pointXY.setY(point2D.getY() + Constants.LATITUDE_OFFSET);
        ArrayList<PointXY> pointXies = new ArrayList<PointXY>();
        pointXies.add(pointXY);
        upload.setPoints(pointXies);
//        upload.setUserAccount(UserInfo.getUserName());
//        upload.setUserName(UserInfo.getRealName());
        UploadPhotoAndPoints uploadPhotoAndPoints = new UploadPhotoAndPoints(file, upload);
        UploadPhotoPointsThread uploadPhotoPointsThread = new UploadPhotoPointsThread(uploadPhotoAndPoints);
        new Thread(uploadPhotoPointsThread).start();
    }

    @SuppressLint("HandlerLeak")
    Handler handler = new Handler()
    {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case Constants.UPLOAD_SUCCESS:
                    Toast.makeText(UploadPhoto.this, "文件上传成功", Toast.LENGTH_LONG).show();
                    finish();
                    break;
                case Constants.UPLOAD_FAIL:
                    Toast.makeText(UploadPhoto.this, "文件上传失败", Toast.LENGTH_LONG).show();
                    break;
                default:
                    break;
            }
        }

    };

}
